package com.hspedu.Homework;
/*
 *@auther will
 *@version 1.0
 */

import java.util.Arrays;
import java.util.Comparator;

public class ArrayExercise {
    public static void main(String[] args) {
        Book[] books = new Book[4];
        books[0] = new Book("红楼梦",100);
        books[1] = new Book("金瓶梅10",90);
        books[2] = new Book("青年文摘20年",5);
        books[3] = new Book("java从入门到放弃",300);
//        System.out.println(books[0].getPrice());
        Arrays.sort(books, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
               Book book1= (Book)o1;
               Book book2= (Book)o2;
               double priceVal =  book2.getName().length()- book1.getName().length();
               if (priceVal>0){
                   return -1;
               }else if(priceVal<0){return 1;}
               else return 0;
            }
        });
        System.out.println(Arrays.toString(books));
    }
}

class Book{
    private String name;
    private int price;

    public Book(String name, int price) {
        this.name = name;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public int getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}
